home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / latex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  34.8 KB  |  1,249 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/latex.c 1.16 1996/02/24 14:53:25 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Writes the Latex output.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21.  
  22. #ifndef min
  23. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  24. #endif
  25.  
  26. #include "memory.h"
  27. #include "datatype.h"
  28. #include "cxref.h"
  29.  
  30. /*+ The name of the output tex file that includes each of the others. +*/
  31. #define LATEX_FILE        ".tex"
  32. #define LATEX_FILE_BACKUP ".tex~"
  33.  
  34. /*+ The name of the output tex file that contains the appendix. +*/
  35. #define LATEX_APDX_FILE        ".apdx.tex"
  36.  
  37. /*+ The type of LaTeX output to produce. +*/
  38. extern int option_latex;
  39.  
  40. /*+ The name of the directory for the output. +*/
  41. extern char* option_odir;
  42.  
  43. /*+ The base name of the file for the output. +*/
  44. extern char* option_name;
  45.  
  46. extern char *latex_page_style,*latex_fonts_style;
  47.  
  48. static void WriteLatexFilePart(File file);
  49. static void WriteLatexInclude(Include inc);
  50. static void WriteLatexSubInclude(Include inc,int depth);
  51. static void WriteLatexDefine(Define def);
  52. static void WriteLatexTypedef(Typedef type,char* filename);
  53. static void WriteLatexStructUnion(StructUnion su, int depth);
  54. static void WriteLatexVariable(Variable var,char* filename);
  55. static void WriteLatexFunction(Function func,char* filename);
  56.  
  57. static void WriteLatexDocument(char* name);
  58. static void WriteLatexPreamble(FILE* f);
  59. static void WriteLatexPostamble(FILE* f);
  60. static void AddLatexAppendix(void);
  61.  
  62. static char* latex(char* c);
  63.  
  64. /*+ The output file for the latex. +*/
  65. static FILE* of;
  66.  
  67. /*+ Counts the lines in a table to insert breaks. +*/
  68. static int countlines=0;
  69.  
  70. /*++++++++++++++++++++++++++++++++++++++
  71.   Write a Latex file for a complete File structure and all components.
  72.  
  73.   File file The File structure to output.
  74.   ++++++++++++++++++++++++++++++++++++++*/
  75.  
  76. void WriteLatexFile(File file)
  77. {
  78.  char* ofile;
  79.  
  80.  /* Write the including file. */
  81.  
  82.  WriteLatexDocument(file->name);
  83.  
  84.  /* Open the file */
  85.  
  86.  ofile=ConcatStrings(4,option_odir,"/",file->name,LATEX_FILE);
  87.  
  88.  of=fopen(ofile,"w");
  89.  
  90.  if(!of)
  91.    {fprintf(stderr,"cxref: Failed to open the LaTeX output file '%s'\n",ofile);exit(1);}
  92.  
  93.  /* Write out a header. */
  94.  
  95.  fputs("% This LaTeX file generated by cxref\n",of);
  96.  fputs("% cxref program (c) Andrew M. Bishop 1995,96.\n",of);
  97.  fputs("\n",of);
  98.  
  99.  /*+ The file structure is broken into its components and they are each written out. +*/
  100.  
  101.  WriteLatexFilePart(file);
  102.  
  103.  if(file->includes)
  104.    {
  105.     Include inc =file->includes;
  106.     fprintf(of,"\\subsection*{Included Files}\n\n");
  107.     do{
  108.        if(inc!=file->includes)
  109.           fprintf(of,"\\medskip\n");
  110.        WriteLatexInclude(inc);
  111.       }
  112.     while((inc=inc->next));
  113.    }
  114.  
  115.  if(file->defines)
  116.    {
  117.     Define def =file->defines;
  118.     fprintf(of,"\\subsection*{Preprocessor definitions}\n\n");
  119.     do{
  120.        if(def!=file->defines)
  121.           fprintf(of,"\\medskip\n");
  122.        WriteLatexDefine(def);
  123.       }
  124.     while((def=def->next));
  125.    }
  126.  
  127.  if(file->typedefs)
  128.    {
  129.     Typedef type=file->typedefs;
  130.     fprintf(of,"\\subsection{Type definitions}\n\n");
  131.     do{
  132.        WriteLatexTypedef(type,file->name);
  133.       }
  134.     while((type=type->next));
  135.    }
  136.  
  137.  if(file->variables)
  138.    {
  139.     int any_to_mention=0;
  140.     Variable var=file->variables;
  141.  
  142.     do{
  143.        if(var->scope&(GLOBAL|LOCAL|EXTERNAL))
  144.           any_to_mention=1;
  145.       }
  146.     while((var=var->next));
  147.  
  148.     if(any_to_mention)
  149.       {
  150.        int first_ext=1,first_local=1;
  151.        Variable var=file->variables;
  152.        fprintf(of,"\\subsection{Variables}\n\n");
  153.        do{
  154.           if(var->scope&GLOBAL)
  155.              WriteLatexVariable(var,file->name);
  156.          }
  157.        while((var=var->next));
  158.        var=file->variables;
  159.        do{
  160.           if(var->scope&EXTERNAL && !(var->scope&GLOBAL))
  161.             {
  162.              if(first_ext)
  163.                {fprintf(of,"\\subsubsection{External Variables}\n\n"); first_ext=0;}
  164.              else
  165.                 fprintf(of,"\\medskip\n");
  166.              WriteLatexVariable(var,file->name);
  167.             }
  168.          }
  169.        while((var=var->next));
  170.        var=file->variables;
  171.        do{
  172.           if(var->scope&LOCAL)
  173.             {
  174.              if(first_local)
  175.                {fprintf(of,"\\subsubsection{Local Variables}\n\n"); first_local=0;}
  176.              else
  177.                 fprintf(of,"\\medskip\n");
  178.              WriteLatexVariable(var,file->name);
  179.             }
  180.          }
  181.        while((var=var->next));
  182.       }
  183.    }
  184.  
  185.  if(file->functions)
  186.    {
  187.     Function func=file->functions;
  188.     fprintf(of,"\\subsection{Functions}\n\n");
  189.     do{
  190.        if(func->scope&GLOBAL)
  191.           WriteLatexFunction(func,file->name);
  192.       }
  193.     while((func=func->next));
  194.     func=file->functions;
  195.     do{
  196.        if(func->scope&LOCAL)
  197.           WriteLatexFunction(func,file->name);
  198.       }
  199.     while((func=func->next));
  200.    }
  201.  
  202.  fclose(of);
  203.  
  204. /* Clear the memory in latex() */
  205.  
  206.  latex(NULL); latex(NULL); latex(NULL); latex(NULL);
  207. }
  208.  
  209.  
  210. /*++++++++++++++++++++++++++++++++++++++
  211.   Write a File structure out.
  212.  
  213.   File file The File to output.
  214.   ++++++++++++++++++++++++++++++++++++++*/
  215.  
  216. static void WriteLatexFilePart(File file)
  217. {
  218.  int i;
  219.  
  220.  fprintf(of,"\\markboth{File %s}{File %s}\n",latex(file->name),latex(file->name));
  221.  fprintf(of,"\\section{File %s}\n",latex(file->name));
  222.  fprintf(of,"\\label{file_%s}\n\n",file->name);
  223.  
  224.  if(file->comment)
  225.    {
  226.     char *rcs1=strstr(file->comment,"$Header"),*rcs2=NULL;
  227.     if(rcs1)
  228.       {
  229.        rcs2=strstr(&rcs1[1],"$");
  230.        if(rcs2)
  231.          {
  232.           rcs2[0]=0;
  233.           fprintf(of,"{\\bf RCS %s}\n\n",latex(&rcs1[1]));
  234.           fprintf(of,"\\smallskip\n");
  235.           rcs2[0]='$';
  236.          }
  237.       }
  238.     if(rcs2)
  239.        fprintf(of,"%s\n\n",latex(&rcs2[2]));
  240.     else
  241.        fprintf(of,"%s\n\n",latex(file->comment));
  242.    }
  243.  
  244.  if(file->inc_in.n)
  245.    {
  246.     int i;
  247.  
  248.     if(file->comment)
  249.        fprintf(of,"\\medskip\n");
  250.     fprintf(of,"\\begin{cxreftabii}\nIncluded in:");
  251.     for(i=0;i<file->inc_in.n;i++)
  252.       {/* Allow a break in every 8 (or so) items to stop allow the table to break over the page. */
  253.        if(min(i,file->inc_in.n-i)%8 == 4)
  254.           fprintf(of,"\\cxreftabbreak{cxreftabii}\n");
  255.        fprintf(of,"\\ & %s & \\cxreffile{%s}\\\\\n",latex(file->inc_in.s[i]),file->inc_in.s[i]);
  256.       }
  257.     fprintf(of,"\\end{cxreftabii}\n\n");
  258.    }
  259.  
  260.  if(file->f_refs.n || file->v_refs.n)
  261.    {
  262.     int tabcount=0;
  263.     fprintf(of,"\\smallskip\n");
  264.     fprintf(of,"\\begin{cxreftabiii}\n");
  265.  
  266.     if(file->f_refs.n)
  267.       {
  268.        int others=0;
  269.        for(i=0;i<file->f_refs.n;i++)
  270.          {
  271.           char *temp=strchr(file->f_refs.s[i],':'),*filen;
  272.           if(temp)
  273.             {
  274.              temp[-1]=0;
  275.              filen=&temp[2];
  276.              if(++tabcount%8 == 4)
  277.                 fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  278.              if(i==others)
  279.                 fprintf(of,"Refs Func:");
  280.              fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(file->f_refs.s[i]),latex(filen),file->f_refs.s[i],filen);
  281.              temp[-1]=' ';
  282.             }
  283.           else
  284.              others++;
  285.          }
  286.  
  287.        if(others)
  288.          {
  289.           if(others==file->f_refs.n)
  290.              fprintf(of,"Refs Func:");
  291.           fprintf(of,"\\ & \\twocoltabiii{");
  292.           for(i=0;i<file->f_refs.n;i++)
  293.              if(!strchr(file->f_refs.s[i],':'))
  294.                 fprintf(of,--others?" %s(),":" %s()",latex(file->f_refs.s[i]));
  295.           fprintf(of,"} &\\\\\n");
  296.          }
  297.       }
  298.  
  299.     if(file->v_refs.n)
  300.       {
  301.        int others=0;
  302.        for(i=0;i<file->v_refs.n;i++)
  303.          {
  304.           char *temp=strchr(file->v_refs.s[i],':'),*filen;
  305.           if(temp)
  306.             {
  307.              temp[-1]=0;
  308.              filen=&temp[2];
  309.              if(++tabcount%8 == 4)
  310.                 fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  311.              if(i==others)
  312.                 fprintf(of,"Refs Var:");
  313.              fprintf(of,"\\ & %s & %s & \\cxrefvar{%s}{%s}\\\\\n",latex(file->v_refs.s[i]),latex(filen),file->v_refs.s[i],filen);
  314.              temp[-1]=' ';
  315.             }
  316.           else
  317.              others++;
  318.          }
  319.  
  320.        if(others)
  321.          {
  322.           if(others==file->v_refs.n)
  323.              fprintf(of,"Refs Var:");
  324.           fprintf(of,"\\ & \\twocoltabiii{");
  325.           for(i=0;i<file->v_refs.n;i++)
  326.              if(!strchr(file->v_refs.s[i],':'))
  327.                 fprintf(of,--others?" %s,":" %s",latex(file->v_refs.s[i]));
  328.           fprintf(of,"} &\\\\\n");
  329.          }
  330.       }
  331.  
  332.     fprintf(of,"\\end{cxreftabiii}\n\n");
  333.    }
  334. }
  335.  
  336.  
  337. /*++++++++++++++++++++++++++++++++++++++
  338.   Write an Include structure out.
  339.  
  340.   Include inc The Include structure to output.
  341.   ++++++++++++++++++++++++++++++++++++++*/
  342.  
  343. static void WriteLatexInclude(Include inc)
  344. {
  345.  if(inc->comment)
  346.     fprintf(of,"%s\n\n\\smallskip\n",latex(inc->comment));
  347.  
  348.  fprintf(of,"\\begin{cxreftabi}\n"); countlines=1;
  349.  
  350.  if(inc->scope==LOCAL)
  351.     fprintf(of,"{\\stt \\#include \"%s\"} &\\cxreffile{%s}\\\\\n",latex(inc->name),inc->name);
  352.  else
  353.     fprintf(of,"{\\stt \\#include <%s>} &\\\\\n",latex(inc->name));
  354.  
  355.  if(inc->includes)
  356.     WriteLatexSubInclude(inc->includes,1);
  357.  
  358.  fprintf(of,"\\end{cxreftabi}\n\n");
  359. }
  360.  
  361.  
  362. /*++++++++++++++++++++++++++++++++++++++
  363.   Write an Sub Include structure out. (An include structure that is included from another file.)
  364.  
  365.   Include inc The Include structure to output.
  366.  
  367.   int depth The depth of the include hierarchy.
  368.   ++++++++++++++++++++++++++++++++++++++*/
  369.  
  370. static void WriteLatexSubInclude(Include inc,int depth)
  371. {
  372.  while(inc)
  373.    {
  374.     if(countlines++%8==4)
  375.        fprintf(of,"\\cxreftabbreak{cxreftabi}\n");
  376.  
  377.     fprintf(of,"\\hspace*{%3.1fin}",0.2*depth);
  378.  
  379.     if(inc->scope==LOCAL)
  380.        fprintf(of,"{\\stt \\#include \"%s\"} &\\cxreffile{%s}\\\\\n",latex(inc->name),inc->name);
  381.     else
  382.        fprintf(of,"{\\stt \\#include <%s>} &\\\\\n",latex(inc->name));
  383.  
  384.     if(inc->includes)
  385.        WriteLatexSubInclude(inc->includes,depth+1);
  386.  
  387.     inc=inc->next;
  388.    }
  389. }
  390.  
  391.  
  392. /*++++++++++++++++++++++++++++++++++++++
  393.   Write a Define structure out.
  394.  
  395.   Define def The Define structure to output.
  396.   ++++++++++++++++++++++++++++++++++++++*/
  397.  
  398. static void WriteLatexDefine(Define def)
  399. {
  400.  int i;
  401.  int pargs=0;
  402.  
  403.  if(def->comment)
  404.     fprintf(of,"%s\n\n\\smallskip\n",latex(def->comment));
  405.  
  406.  fprintf(of,"{\\stt \\#define %s",latex(def->name));
  407.  
  408.  if(def->value)
  409.     fprintf(of," %s",latex(def->value));
  410.  
  411.  if(def->args.n)
  412.    {
  413.     fprintf(of,"( ");
  414.     for(i=0;i<def->args.n;i++)
  415.        fprintf(of,i?", %s":"%s",latex(def->args.s1[i]));
  416.     fprintf(of," )");
  417.    }
  418.  fprintf(of,"}\n\n");
  419.  
  420.  for(i=0;i<def->args.n;i++)
  421.     if(def->args.s2[i])
  422.        pargs=1;
  423.  
  424.  if(pargs)
  425.    {
  426.     fprintf(of,"\\smallskip\n");
  427.     fprintf(of,"\\begin{cxrefarglist}\n");
  428.     for(i=0;i<def->args.n;i++)
  429.        fprintf(of,"\\cxrefargitem{%s} %s\n",latex(def->args.s1[i]),def->args.s2[i]?latex(def->args.s2[i]):"\\ ");
  430.     fprintf(of,"\\end{cxrefarglist}\n\n");
  431.    }
  432. }
  433.  
  434.  
  435. /*++++++++++++++++++++++++++++++++++++++
  436.   Write a Typedef structure out.
  437.  
  438.   Typedef type The Typedef structure to output.
  439.  
  440.   char* filename The name of the file that is being processed (required for the cross reference label).
  441.   ++++++++++++++++++++++++++++++++++++++*/
  442.  
  443. static void WriteLatexTypedef(Typedef type,char* filename)
  444. {
  445.  if(type->type)
  446.     fprintf(of,"\\subsubsection{Typedef %s}\n",latex(type->name));
  447.  else
  448.     fprintf(of,"\\subsubsection{Type %s}\n",latex(type->name));
  449.  
  450.  if(!strncmp("enum",type->name,4))
  451.     fprintf(of,"\\label{type_enum_%s_%s}\n\n",&type->name[5],filename);
  452.  else
  453.     if(!strncmp("union",type->name,5))
  454.        fprintf(of,"\\label{type_union_%s_%s}\n\n",&type->name[6],filename);
  455.     else
  456.        if(!strncmp("struct",type->name,6))
  457.           fprintf(of,"\\label{type_struct_%s_%s}\n\n",&type->name[7],filename);
  458.        else
  459.           fprintf(of,"\\label{type_%s_%s}\n\n",type->name,filename);
  460.  
  461.  if(type->comment)
  462.     fprintf(of,"%s\n\n\\smallskip\n",latex(type->comment));
  463.  
  464.  if(type->type)
  465.     fprintf(of,"{\\stt typedef %s}\n\n",latex(type->type));
  466.  
  467.  if(type->sutype)
  468.    {
  469.     fprintf(of,"\\smallskip\n");
  470.     fprintf(of,"\\begin{cxreftabiia}\n"); countlines=0;
  471.     WriteLatexStructUnion(type->sutype,0);
  472.     fprintf(of,"\\end{cxreftabiia}\n\n");
  473.    }
  474.  else
  475.     if(type->typexref)
  476.       {
  477.        fprintf(of,"\\smallskip\n");
  478.        fprintf(of,"\\begin{cxreftabii}\n");
  479.        if(type->typexref->type)
  480.           fprintf(of,"See:& Typedef %s & \\cxreftype{%s}{%s}\\\\\n",latex(type->typexref->name),type->typexref->name,filename);
  481.        else
  482.           if(!strncmp("enum",type->typexref->name,4))
  483.              fprintf(of,"See:& Type %s & \\cxreftype{enum_%s}{%s}\\\\\n",latex(type->typexref->name),&type->typexref->name[5],filename);
  484.           else
  485.              if(!strncmp("union",type->typexref->name,5))
  486.                 fprintf(of,"See:& Type %s & \\cxreftype{union_%s}{%s}\\\\\n",latex(type->typexref->name),&type->typexref->name[6],filename);
  487.              else
  488.                 if(!strncmp("struct",type->typexref->name,6))
  489.                    fprintf(of,"See:& Type %s & \\cxreftype{struct_%s}{%s}\\\\\n",latex(type->typexref->name),&type->typexref->name[7],filename);
  490.        fprintf(of,"\\end{cxreftabii}\n\n");
  491.       }
  492. }
  493.  
  494.  
  495. /*++++++++++++++++++++++++++++++++++++++
  496.   Write a structure / union structure out.
  497.  
  498.   StructUnion su The structure / union to write.
  499.  
  500.   int depth The current depth within the structure.
  501.   ++++++++++++++++++++++++++++++++++++++*/
  502.  
  503. static void WriteLatexStructUnion(StructUnion su, int depth)
  504. {
  505.  int i;
  506.  char* splitsu=NULL;
  507.  
  508.  splitsu=strstr(su->name,"{...}");
  509.  if(splitsu) splitsu[-1]=0;
  510.  
  511.  if(countlines++%8==4)
  512.     fprintf(of,"\\cxreftabbreak{cxreftabiia}\n");
  513.  fprintf(of,"\\hspace*{%3.1fin}",0.2*depth);
  514.  
  515.  if(depth && su->comment)
  516.     fprintf(of,"{\\stt %s} & %s \\\\\n",latex(su->name),latex(su->comment));
  517.  else
  518.     fprintf(of,"{\\stt %s} &\\\\\n",latex(su->name));
  519.  
  520.  if(su->comps)
  521.    {
  522.     fprintf(of,"\\hspace*{%3.1fin}",0.1+0.2*depth);
  523.     fprintf(of,"{\\stt \\{} &\\\\\n");
  524.  
  525.     for(i=0;i<su->n_comp;i++)
  526.        WriteLatexStructUnion(su->comps[i],depth+1);
  527.  
  528.     fprintf(of,"\\hspace*{%3.1fin}",0.1+0.2*depth);
  529.     fprintf(of,"{\\stt \\}} &\\\\\n");
  530.     if(splitsu)
  531.       {
  532.        fprintf(of,"\\hspace*{%3.1fin}",0.1+0.2*depth);
  533.        fprintf(of,"{\\stt %s} &\\\\\n",latex(&splitsu[6]));
  534.       }
  535.    }
  536.  
  537.  if(splitsu) splitsu[-1]=' ';
  538. }
  539.  
  540.  
  541. /*++++++++++++++++++++++++++++++++++++++
  542.   Write a Variable structure out.
  543.  
  544.   Variable var The Variable structure to output.
  545.  
  546.   char* filename The name of the file that is being processed (required for the cross reference label).
  547.   ++++++++++++++++++++++++++++++++++++++*/
  548.  
  549. static void WriteLatexVariable(Variable var,char* filename)
  550. {
  551.  int i;
  552.  
  553.  if(var->scope&GLOBAL)
  554.     fprintf(of,"\\subsubsection{Variable %s}\n",latex(var->name));
  555.  else
  556.     fprintf(of,"{\\bf %s}\n",latex(var->name));
  557.  
  558.  fprintf(of,"\\label{var_%s_%s}\n\n",var->name,filename);
  559.  
  560.  if(var->comment)
  561.     fprintf(of,"%s\n\n\\smallskip\n",latex(var->comment));
  562.  
  563.  switch(var->scope)
  564.    {
  565.    case LOCAL:
  566.     fprintf(of,"{\\stt static %s}\n\n",latex(var->type));
  567.     break;
  568.    case EXTERNAL:
  569.     fprintf(of,"{\\stt extern %s}\n\n",latex(var->type));
  570.     break;
  571.    default:
  572.     fprintf(of,"{\\stt %s}\n\n",latex(var->type));
  573.     break;
  574.    }
  575.  
  576.  if(var->scope&(GLOBAL|LOCAL))
  577.    {
  578.     if(var->used.n || var->visible.n)
  579.       {
  580.        fprintf(of,"\\smallskip\n");
  581.        fprintf(of,"\\begin{cxreftabiii}\n");
  582.  
  583.        for(i=0;i<var->visible.n;i++)
  584.          {
  585.           if(min(i,var->visible.n+var->used.n-i)%8 == 4)
  586.              fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  587.           if(i==0) fprintf(of,"Visible in:");
  588.           fprintf(of,"\\ & %s & \\ & \\cxreffile{%s}\\\\\n",latex(var->visible.s[i]),var->visible.s[i]);
  589.          }
  590.  
  591.        for(i=0;i<var->used.n;i++)
  592.          {
  593.           if(min(i,var->visible.n+var->used.n-i)%8 == 4)
  594.              fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  595.           if(i==0) fprintf(of,"Used in:");
  596.           if(var->used.s[i][0]=='$')
  597.              fprintf(of,"\\ & %s & \\ & \\cxreffile{%s}\\\\\n",latex(&var->used.s[i][1]),&var->used.s[i][1]);
  598.           else
  599.             {
  600.              char *temp=strchr(var->used.s[i],':'),*file;
  601.              temp[-1]=0;
  602.              file=&temp[2];
  603.              if(var->scope&LOCAL)
  604.                 fprintf(of,"\\ & %s() & \\ & \\cxreffunc{%s}{%s}\\\\\n",latex(var->used.s[i]),var->used.s[i],file);
  605.              else
  606.                 fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(var->used.s[i]),latex(file),var->used.s[i],file);
  607.              temp[-1]=' ';
  608.             }
  609.          }
  610.  
  611.        fprintf(of,"\\end{cxreftabiii}\n\n");
  612.       }
  613.    }
  614.  else
  615.     if(var->scope&EXTERNAL && var->defined)
  616.       {
  617.        fprintf(of,"\\smallskip\n");
  618.        fprintf(of,"\\begin{cxreftabiii}\n");
  619.        fprintf(of,"Defined in:& %s & \\ & \\cxrefvar{%s}{%s}",latex(var->defined),var->name,var->defined);
  620.        fprintf(of,"\\end{cxreftabiii}\n\n");
  621.       }
  622. }
  623.  
  624.  
  625. /*++++++++++++++++++++++++++++++++++++++
  626.   Write a Function structure out.
  627.  
  628.   Function func The Function structure to output.
  629.  
  630.   char* filename The name of the file that is being processed (required for the cross reference label).
  631.   ++++++++++++++++++++++++++++++++++++++*/
  632.  
  633. static void WriteLatexFunction(Function func,char* filename)
  634. {
  635.  int i,pret,pargs;
  636.  char* comment2=NULL,*type;
  637.  
  638.  if(func->scope&GLOBAL)
  639.     fprintf(of,"\\subsubsection{Global Function %s()}\n",latex(func->name));
  640.  else
  641.     fprintf(of,"\\subsubsection{Local Function %s()}\n",latex(func->name));
  642.  fprintf(of,"\\label{func_%s_%s}\n\n",func->name,filename);
  643.  
  644.  if(func->comment)
  645.    {
  646.     comment2=strstr(func->comment,"\n\n");
  647.     if(comment2)
  648.        comment2[0]=0;
  649.     fprintf(of,"%s\n\n",latex(func->comment));
  650.     fprintf(of,"\\smallskip\n");
  651.    }
  652.  
  653.  fprintf(of,"{\\stt ");
  654.  switch(func->scope)
  655.    {
  656.    case LOCAL:           fprintf(of,"static "); break;
  657.    case LOCAL+INLINED:   fprintf(of,"static inline "); break;
  658.    case INLINED:         fprintf(of,"inline "); break;
  659.    default:              ;
  660.    }
  661.  
  662.  if((type=strstr(func->type,"()")))
  663.     type[0]=0;
  664.  fprintf(of,"%s ( ",latex(func->type));
  665.  
  666.  for(i=0;i<func->args.n;i++)
  667.     fprintf(of,i?", %s":"%s",latex(func->args.s1[i]));
  668.  
  669.  if(type)
  670.    {fprintf(of," %s}\n\n",&type[1]);type[0]='(';}
  671.  else
  672.     fprintf(of," )}\n\n");
  673.  
  674.  pret =strncmp("void ",func->type,5) && func->cret;
  675.  for(pargs=0,i=0;i<func->args.n;i++)
  676.     pargs = pargs || ( strcmp("void",func->args.s1[i]) && func->args.s2[i] );
  677.  
  678.  if(pret || pargs)
  679.    {
  680.     fprintf(of,"\\smallskip\n");
  681.     fprintf(of,"\\begin{cxrefarglist}\n");
  682.     if(pret)
  683.        fprintf(of,"\\cxrefargitem{%s} %s\n",latex(func->type),func->cret?latex(func->cret):"\\ ");
  684.     if(pargs)
  685.        for(i=0;i<func->args.n;i++)
  686.           fprintf(of,"\\cxrefargitem{%s} %s\n",latex(func->args.s1[i]),func->args.s2[i]?latex(func->args.s2[i]):"\\ ");
  687.     fprintf(of,"\\end{cxrefarglist}\n\n");
  688.    }
  689.  
  690.  if(comment2)
  691.    {
  692.     fprintf(of,"\\smallskip\n");
  693.     fprintf(of,"%s\n\n",latex(&comment2[2]));
  694.     comment2[0]='\n';
  695.    }
  696.  
  697.  if(func->protofile || func->calls.n || func->called.n || func->used.n || func->f_refs.n || func->v_refs.n)
  698.    {
  699.     int tabcount=func->protofile?1:0;
  700.     fprintf(of,"\\smallskip\n");
  701.     fprintf(of,"\\begin{cxreftabiii}\n");
  702.  
  703.     if(func->protofile)
  704.        fprintf(of,"Prototype:& %s & \\ & \\cxreffile{%s}\\\\\n",latex(func->protofile),func->protofile);
  705.  
  706.     if(func->calls.n)
  707.       {
  708.        int others=0;
  709.        for(i=0;i<func->calls.n;i++)
  710.          {
  711.           char *temp=strchr(func->calls.s[i],':'),*file;
  712.           if(temp)
  713.             {
  714.              temp[-1]=0;
  715.              file=&temp[2];
  716.              if(++tabcount%8 == 4)
  717.                 fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  718.              if(i==others)
  719.                 fprintf(of,"Calls:");
  720.              fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func->calls.s[i]),latex(file),func->calls.s[i],file);
  721.              temp[-1]=' ';
  722.             }
  723.           else
  724.              others++;
  725.          }
  726.  
  727.        if(others)
  728.          {
  729.           if(others==func->calls.n)
  730.              fprintf(of,"Calls:");
  731.           fprintf(of,"\\ & \\twocoltabiii{");
  732.           for(i=0;i<func->calls.n;i++)
  733.              if(!strchr(func->calls.s[i],':'))
  734.                 fprintf(of,--others?" %s(),":" %s()",latex(func->calls.s[i]));
  735.           fprintf(of,"} &\\\\\n");
  736.          }
  737.       }
  738.  
  739.     if(func->called.n)
  740.       {
  741.        for(i=0;i<func->called.n;i++)
  742.          {
  743.           char *temp=strchr(func->called.s[i],':'),*file;
  744.           temp[-1]=0;
  745.           file=&temp[2];
  746.           if(++tabcount%8 == 4)
  747.              fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  748.           if(i==0)
  749.              fprintf(of,"Called by:");
  750.           fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func->called.s[i]),latex(file),func->called.s[i],file);
  751.           temp[-1]=' ';
  752.          }
  753.       }
  754.  
  755.     if(func->used.n)
  756.       {
  757.        for(i=0;i<func->used.n;i++)
  758.          {
  759.           if(++tabcount%8 == 4)
  760.              fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  761.           if(i==0)
  762.              fprintf(of,"Used in:");
  763.           if(func->used.s[i][0]=='$')
  764.              fprintf(of,"\\ & %s & \\ & \\cxreffile{%s}\\\\\n",latex(&func->used.s[i][1]),&func->used.s[i][1]);
  765.           else
  766.             {
  767.              char *temp=strchr(func->used.s[i],':'),*file;
  768.              temp[-1]=0;
  769.              file=&temp[2];
  770.              fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func->used.s[i]),latex(file),func->used.s[i],file);
  771.              temp[-1]=' ';
  772.             }
  773.          }
  774.       }
  775.  
  776.     if(func->f_refs.n)
  777.       {
  778.        int others=0;
  779.        for(i=0;i<func->f_refs.n;i++)
  780.          {
  781.           char *temp=strchr(func->f_refs.s[i],':'),*file;
  782.           if(temp)
  783.             {
  784.              temp[-1]=0;
  785.              file=&temp[2];
  786.              if(++tabcount%8 == 4)
  787.                 fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  788.              if(i==others)
  789.                 fprintf(of,"Refs Func:");
  790.              fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func->f_refs.s[i]),latex(file),func->f_refs.s[i],file);
  791.              temp[-1]=' ';
  792.             }
  793.           else
  794.              others++;
  795.          }
  796.  
  797.        if(others)
  798.          {
  799.           if(others==func->f_refs.n)
  800.              fprintf(of,"Refs Func:");
  801.           fprintf(of,"\\ & \\twocoltabiii{");
  802.           for(i=0;i<func->f_refs.n;i++)
  803.              if(!strchr(func->f_refs.s[i],':'))
  804.                 fprintf(of,--others?" %s(),":" %s()",latex(func->f_refs.s[i]));
  805.           fprintf(of,"} &\\\\\n");
  806.          }
  807.       }
  808.  
  809.     if(func->v_refs.n)
  810.       {
  811.        int others=0;
  812.        for(i=0;i<func->v_refs.n;i++)
  813.          {
  814.           char *temp=strchr(func->v_refs.s[i],':'),*file;
  815.           if(temp)
  816.             {
  817.              temp[-1]=0;
  818.              file=&temp[2];
  819.              if(++tabcount%8 == 4)
  820.                 fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  821.              if(i==others)
  822.                 fprintf(of,"Refs Var:");
  823.              fprintf(of,"\\ & %s & %s & \\cxrefvar{%s}{%s}\\\\\n",latex(func->v_refs.s[i]),latex(file),func->v_refs.s[i],file);
  824.              temp[-1]=' ';
  825.             }
  826.           else
  827.              others++;
  828.          }
  829.  
  830.        if(others)
  831.          {
  832.           if(others==func->v_refs.n)
  833.              fprintf(of,"Refs Var:");
  834.           fprintf(of,"\\ & \\twocoltabiii{");
  835.           for(i=0;i<func->v_refs.n;i++)
  836.              if(!strchr(func->v_refs.s[i],':'))
  837.                 fprintf(of,--others?" %s,":" %s",latex(func->v_refs.s[i]));
  838.           fprintf(of,"} &\\\\\n");
  839.          }
  840.       }
  841.  
  842.     fprintf(of,"\\end{cxreftabiii}\n\n");
  843.    }
  844.  
  845. }
  846.  
  847.  
  848. /*++++++++++++++++++++++++++++++++++++++
  849.   Write out a file that will include the current information.
  850.  
  851.   char* name The name of the file.
  852.   ++++++++++++++++++++++++++++++++++++++*/
  853.  
  854. static void WriteLatexDocument(char* name)
  855. {
  856.  FILE *in,*out;
  857.  char line[256];
  858.  int seen=0;
  859.  char *inc_file,*ofile,*ifile;
  860.  
  861.  inc_file=ConcatStrings(3,"\\input{",name,LATEX_FILE"}\n");
  862.  ifile=ConcatStrings(4,option_odir,"/",option_name,LATEX_FILE);
  863.  ofile=ConcatStrings(4,option_odir,"/",option_name,LATEX_FILE_BACKUP);
  864.  
  865.  in =fopen(ifile,"r");
  866.  out=fopen(ofile,"w");
  867.  
  868.  if(!out)
  869.    {fprintf(stderr,"cxref: Failed to open the main LaTeX output file '%s'\n",ofile);exit(1);}
  870.  
  871.  if(!in)
  872.     WriteLatexPreamble(out);
  873.  
  874.  if(in)
  875.     while(fgets(line,256,in))
  876.       {
  877.        if(!strcmp(inc_file,line))
  878.           seen=1;
  879.        if(line[0]=='%' && (!strcmp("% Appendix\n",line) || !strcmp("% Contents\n",line)) && !seen)
  880.          {fputs(inc_file,out);fputs("\n",out);seen=1;}
  881.        fputs(line,out);
  882.       }
  883.  
  884.  if(!in)
  885.    {
  886.     fputs(inc_file,out);
  887.     WriteLatexPostamble(out);
  888.    }
  889.  
  890.  if(in)
  891.    {
  892.     fclose(in);
  893.     unlink(ifile);
  894.    }
  895.  
  896.  fclose(out);
  897.  rename(ofile,ifile);
  898. }
  899.  
  900.  
  901. /*++++++++++++++++++++++++++++++++++++++
  902.   Write out a standard pre-amble. This sets up the page style and defines some macros.
  903.  
  904.   FILE* f The file to write the pre amble to.
  905.   ++++++++++++++++++++++++++++++++++++++*/
  906.  
  907. static void WriteLatexPreamble(FILE* f)
  908. {
  909.  struct stat stat_buf;
  910.  char* fname;
  911.  
  912.  fname=ConcatStrings(2,option_odir,"/fonts.sty");
  913.  if(stat(fname,&stat_buf)==-1)
  914.    {
  915.     FILE* file=fopen(fname,"w");
  916.     if(!file)
  917.       {fprintf(stderr,"cxref: Cannot write the LaTeX style file '%s'\n",fname);exit(1);}
  918.     fputs(latex_fonts_style,file);
  919.     fclose(file);
  920.    }
  921.  
  922.  fname=ConcatStrings(2,option_odir,"/page.sty");
  923.  if(stat(fname,&stat_buf)==-1)
  924.    {
  925.     FILE* file=fopen(fname,"w");
  926.     if(!file)
  927.       {fprintf(stderr,"cxref: Cannot write the LaTeX style file '%s'\n",fname);exit(1);}
  928.     fputs(latex_page_style,file);
  929.     fclose(file);
  930.    }
  931.  
  932.  fputs("% This LaTeX file generated by cxref\n",f);
  933.  fputs("% cxref program (c) Andrew M. Bishop 1995,96.\n",f);
  934.  fputs("\n",f);
  935.  if(option_latex==1)
  936.     fputs("\\documentstyle[fonts,page]{report}\n",f);
  937.  else
  938.    {
  939.     fputs("\\documentclass{report}\n",f);
  940.     fputs("\\usepackage{fonts,page}\n",f);
  941.    }
  942.  fputs("\\pagestyle{myheadings}\n",f);
  943.  fputs("\\begin{document}\n",f);
  944.  fputs("\n",f);
  945.  fputs("\\setcounter{secnumdepth}{3}\n",f);
  946.  fputs("\\setcounter{tocdepth}{3}\n",f);
  947.  fputs("\n",f);
  948.  fputs("\\setlength{\\parindent}{0in}\n",f);
  949.  fputs("\\setlength{\\tabcolsep}{0in}\n",f);
  950.  fputs("\n",f);
  951.  fputs("\\newcommand{\\stt}{\\small\\tt}\n",f);
  952.  fputs("\n",f);
  953.  fputs("\\newenvironment{cxrefarglist}{\\begin{list}{}{\\leftmargin=2.25in \\labelsep=0in \\labelwidth=2.25in \\itemsep=0pt \\parsep=3pt \\topsep=3pt}}{\\end{list}}\n",f);
  954.  fputs("\\newcommand{\\cxrefargitem}[1]{\\item[\\parbox{2.25in}{$\\cdot$\\hspace*{0.1in}{\\stt #1}}]}\n",f);
  955.  fputs("\n",f);
  956.  fputs("\\newenvironment{cxreftabi}{\\begin{tabular}{{p{5.0in}l}}}{\\end{tabular}}\n",f);
  957.  fputs("\\newenvironment{cxreftabii}{\\begin{tabular}{{p{0.75in}p{4.25in}l}}}{\\end{tabular}}\n",f);
  958.  fputs("\\newenvironment{cxreftabiia}{\\begin{tabular}{{p{2.5in}p{3.5in}}}}{\\end{tabular}}\n",f);
  959.  fputs("\\newenvironment{cxreftabiib}{\\begin{tabular}{{p{2.5in}p{2.5in}l}}}{\\end{tabular}}\n",f);
  960.  fputs("\\newenvironment{cxreftabiii}{\\begin{tabular}{{p{0.75in}p{2.25in}p{2.0in}l}}}{\\end{tabular}}\n",f);
  961.  fputs("\\newcommand{\\twocoltabiii}[1]{\\multicolumn{2}{p{4.25in}}{#1}}\n",f);
  962.  fputs("\n",f);
  963.  fputs("\\newcommand{\\cxreftabbreak}[1]{\\end{#1}\\vskip 0pt\\begin{#1}}\n",f);
  964.  fputs("\n",f);
  965.  fputs("\\newcommand{\\cxreffile}[1]{{\\small\\it (Section \\ref{file_#1})\\/}}\n",f);
  966.  fputs("\\newcommand{\\cxreffunc}[2]{{\\small\\it (Section \\ref{func_#1_#2})\\/}}\n",f);
  967.  fputs("\\newcommand{\\cxrefvar}[2]{{\\small\\it (Section \\ref{var_#1_#2})\\/}}\n",f);
  968.  fputs("\\newcommand{\\cxreftype}[2]{{\\small\\it (Section \\ref{type_#1_#2})\\/}}\n",f);
  969.  fputs("\n",f);
  970.  fputs("% Begin-Of-Doc\n",f);
  971.  fputs("\n",f);
  972.  fputs("\\chapter{Chapter heading}\n",f);
  973.  fputs("\n",f);
  974. }
  975.  
  976.  
  977. /*++++++++++++++++++++++++++++++++++++++
  978.   Write out a standard post-amble. This includes a table of contents and the end of document marker.
  979.  
  980.   FILE* f The file to write the post amble to.
  981.   ++++++++++++++++++++++++++++++++++++++*/
  982.  
  983. static void WriteLatexPostamble(FILE* f)
  984. {
  985.  fputs("\n",f);
  986.  fputs("% Contents\n",f);
  987.  fputs("\n",f);
  988.  fputs("\\markboth{Contents}{Contents}\n",f);
  989.  fputs("\\tableofcontents\n",f);
  990.  fputs("\n",f);
  991.  fputs("% End-Of-Doc\n",f);
  992.  fputs("\n",f);
  993.  fputs("\\end{document}\n",f);
  994. }
  995.  
  996.  
  997. /*++++++++++++++++++++++++++++++++++++++
  998.   Write out the appendix information.
  999.  
  1000.   StringList* files The list of files to write.
  1001.  
  1002.   StringList* funcs The list of functions to write.
  1003.  
  1004.   StringList* vars The list of variables to write.
  1005.  
  1006.   StringList* types The list of types to write.
  1007.   ++++++++++++++++++++++++++++++++++++++*/
  1008.  
  1009. void WriteLatexAppendix(StringList* files,StringList* funcs,StringList* vars,StringList* types)
  1010. {
  1011.  char* ofile;
  1012.  int i;
  1013.  
  1014.  /* Write the bits to the including file. */
  1015.  
  1016.  AddLatexAppendix();
  1017.  
  1018.  /* Open the file */
  1019.  
  1020.  ofile=ConcatStrings(4,option_odir,"/",option_name,LATEX_APDX_FILE);
  1021.  
  1022.  of=fopen(ofile,"w");
  1023.  
  1024.  if(!of)
  1025.    {fprintf(stderr,"cxref: Failed to open the LaTeX appendix file '%s'\n",ofile);exit(1);}
  1026.  
  1027.  /* Write the file structure out */
  1028.  
  1029.  fprintf(of,"\\chapter{Cross References}\n\n");
  1030.  
  1031.  /* Write out the appendix of files. */
  1032.  
  1033.  if(files->n)
  1034.    {
  1035.     fprintf(of,"\\section{Files}\n");
  1036.     fprintf(of,"\\label{appendix_file}\n\n");
  1037.     fprintf(of,"\\begin{cxreftabiib}\n");
  1038.     for(i=0;i<files->n;i++)
  1039.       {
  1040.        if(min(i,files->n-i)%8 == 4)
  1041.           fprintf(of,"\\cxreftabbreak{cxreftabiib}\n");
  1042.        fprintf(of,"%s & \\ & \\cxreffile{%s}\\\\\n",latex(files->s[i]),files->s[i]);
  1043.       }
  1044.     fprintf(of,"\\end{cxreftabiib}\n\n");
  1045.    }
  1046.  
  1047.  /* Write out the appendix of functions. */
  1048.  
  1049.  if(funcs->n)
  1050.    {
  1051.     fprintf(of,"\\section{Global Functions}\n");
  1052.     fprintf(of,"\\label{appendix_func}\n\n");
  1053.     fprintf(of,"\\begin{cxreftabiib}\n");
  1054.     for(i=0;i<funcs->n;i++)
  1055.       {
  1056.        char *temp=strchr(funcs->s[i],':'),*file;
  1057.        temp[-1]=0;
  1058.        file=&temp[2];
  1059.        if(min(i,funcs->n-i)%8 == 4)
  1060.           fprintf(of,"\\cxreftabbreak{cxreftabiib}\n");
  1061.        fprintf(of,"%s & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(funcs->s[i]),latex(file),funcs->s[i],file);
  1062.        temp[-1]=' ';
  1063.       }
  1064.     fprintf(of,"\\end{cxreftabiib}\n\n");
  1065.    }
  1066.  
  1067.  /* Write out the appendix of variables. */
  1068.  
  1069.  if(vars->n)
  1070.    {
  1071.     fprintf(of,"\\section{Global Variables}\n");
  1072.     fprintf(of,"\\label{appendix_var}\n\n");
  1073.     fprintf(of,"\\begin{cxreftabiib}\n");
  1074.     for(i=0;i<vars->n;i++)
  1075.       {
  1076.        char *temp=strchr(vars->s[i],':'),*file;
  1077.        temp[-1]=0;
  1078.        file=&temp[2];
  1079.        if(min(i,vars->n-i)%8 == 4)
  1080.           fprintf(of,"\\cxreftabbreak{cxreftabiib}\n");
  1081.        fprintf(of,"%s & %s & \\cxrefvar{%s}{%s}\\\\\n",latex(vars->s[i]),latex(file),vars->s[i],file);
  1082.        temp[-1]=' ';
  1083.       }
  1084.     fprintf(of,"\\end{cxreftabiib}\n\n");
  1085.    }
  1086.  
  1087.  /* Write out the appendix of types. */
  1088.  
  1089.  if(types->n)
  1090.    {
  1091.     fprintf(of,"\\section{Defined Types}\n");
  1092.     fprintf(of,"\\label{appendix_type}\n\n");
  1093.     fprintf(of,"\\begin{cxreftabiib}\n");
  1094.     for(i=0;i<types->n;i++)
  1095.       {
  1096.        char *temp=strchr(types->s[i],':'),*file;
  1097.        temp[-1]=0;
  1098.        file=&temp[2];
  1099.        if(min(i,types->n-i)%8 == 4)
  1100.           fprintf(of,"\\cxreftabbreak{cxreftabiib}\n");
  1101.        if(!strncmp("enum",types->s[i],4))
  1102.           fprintf(of,"%s & %s & \\cxreftype{enum_%s}{%s}\\\\\n",latex(types->s[i]),latex(file),&types->s[i][5],file);
  1103.        else
  1104.           if(!strncmp("union",types->s[i],5))
  1105.              fprintf(of,"%s & %s & \\cxreftype{union_%s}{%s}\\\\\n",latex(types->s[i]),latex(file),&types->s[i][6],file);
  1106.           else
  1107.              if(!strncmp("struct",types->s[i],6))
  1108.                 fprintf(of,"%s & %s & \\cxreftype{struct_%s}{%s}\\\\\n",latex(types->s[i]),latex(file),&types->s[i][7],file);
  1109.              else
  1110.                 fprintf(of,"%s & %s & \\cxreftype{%s}{%s}\\\\\n",latex(types->s[i]),latex(file),types->s[i],file);
  1111.        temp[-1]=' ';
  1112.       }
  1113.     fprintf(of,"\\end{cxreftabiib}\n\n");
  1114.    }
  1115.  
  1116.  fclose(of);
  1117.  
  1118. /* Clear the memory in latex() */
  1119.  
  1120.  latex(NULL); latex(NULL); latex(NULL); latex(NULL);
  1121. }
  1122.  
  1123.  
  1124. /*++++++++++++++++++++++++++++++++++++++
  1125.   Add the appendix commands to the output file.
  1126.   ++++++++++++++++++++++++++++++++++++++*/
  1127.  
  1128. static void AddLatexAppendix(void)
  1129. {
  1130.  FILE *in,*out;
  1131.  char line[256];
  1132.  int seen=0;
  1133.  char *inc_file,*ofile,*ifile;
  1134.  
  1135.  inc_file=ConcatStrings(3,"\\input{",option_name,LATEX_APDX_FILE"}\n");
  1136.  ifile=ConcatStrings(4,option_odir,"/",option_name,LATEX_FILE);
  1137.  ofile=ConcatStrings(4,option_odir,"/",option_name,LATEX_FILE_BACKUP);
  1138.  
  1139.  in =fopen(ifile,"r");
  1140.  out=fopen(ofile,"w");
  1141.  
  1142.  if(!in || !out)
  1143.    {fprintf(stderr,"cxref: Failed to open the LaTeX main output file '%s'\n",ofile);exit(1);}
  1144.  
  1145.  while(fgets(line,256,in))
  1146.    {
  1147.     if(!strcmp("% Appendix\n",line))
  1148.        seen=1;
  1149.     if(!strcmp("% Contents\n",line) && !seen)
  1150.       {
  1151.        fputs("% Appendix\n\n",out);
  1152.        fputs("\\appendix\n",out);
  1153.        fputs("\\markboth{Appendix}{Appendix}\n",out);
  1154.        fputs(inc_file,out);
  1155.        fputs("\n",out);
  1156.       }
  1157.     fputs(line,out);
  1158.    }
  1159.  
  1160.  fclose(in);
  1161.  if(!seen)
  1162.     unlink(ifile);
  1163.  
  1164.  fclose(out);
  1165.  if(!seen)
  1166.     rename(ofile,ifile);
  1167. }
  1168.  
  1169.  
  1170. /*++++++++++++++++++++++++++++++++++++++
  1171.   Make the input string safe to output as LaTeX ( not #, $, %, &, \, ^, _, {, }, <, > or ~ ).
  1172.  
  1173.   char* latex Returns a safe LaTeX string.
  1174.  
  1175.   char* c A non-safe LaTeX string.
  1176.  
  1177.   The function can only be called four times in each fprintf() since it returns one of only four static strings.
  1178.   ++++++++++++++++++++++++++++++++++++++*/
  1179.  
  1180. static char* latex(char* c)
  1181. {
  1182.  static char safe[4][256],*malloced[4]={NULL,NULL,NULL,NULL};
  1183.  static int which=0;
  1184.  int i=0,j=0,len=256-12;             /* 12 is the longest possible inserted amount */
  1185.  char* ret;
  1186.  
  1187.  which=(which+1)%4;
  1188.  ret=safe[which];
  1189.  
  1190.  if(malloced[which])
  1191.    {Free(malloced[which]);malloced[which]=NULL;}
  1192.  
  1193.  if(c)
  1194.     do{
  1195.        for(;j<len && c[i];i++)
  1196.           switch(c[i])
  1197.             {
  1198.             case '<':
  1199.             case '>':
  1200.              ret[j++]='$';
  1201.              ret[j++]=c[i];
  1202.              ret[j++]='$';
  1203.              break;
  1204.             case '\\':
  1205.              strcpy(&ret[j],"$\\backslash$");j+=12;
  1206.              break;
  1207.             case '~':
  1208.              strcpy(&ret[j],"$\\sim$");j+=6;
  1209.              break;
  1210.             case '^':
  1211.              strcpy(&ret[j],"$\\wedge$");j+=8;
  1212.              break;
  1213.             case '-':
  1214.              ret[j++]='-';
  1215.              ret[j++]='-';
  1216.              break;
  1217.             case '#':
  1218.             case '$':
  1219.             case '%':
  1220.             case '&':
  1221.             case '_':
  1222.             case '{':
  1223.             case '}':
  1224.              ret[j++]='\\';
  1225.             default:            /* fall through */
  1226.              ret[j++]=c[i];
  1227.             }
  1228.  
  1229.        if(c[i])                 /* Not finished */
  1230.          {
  1231.           unsigned int est_size=(110*strlen(c)*j)/(100*i); /* guess 10% larger than current estimate */
  1232.  
  1233.           if(malloced[which])
  1234.              malloced[which]=Realloc(malloced[which],est_size);
  1235.           else
  1236.             {malloced[which]=Malloc(est_size); strncpy(malloced[which],ret,(unsigned)j);}
  1237.           ret=malloced[which];
  1238.           len=est_size-12;
  1239.          }
  1240.        else
  1241.          {ret[j]=0; ret=NULL;}
  1242.       }
  1243.     while(ret);
  1244.  else
  1245.     safe[which][0]=0;
  1246.  
  1247.  return(malloced[which]?malloced[which]:safe[which]);
  1248. }
  1249.